﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Marketplace;
using Microsoft.Xna.Framework.Media;

namespace EbbinghausWord.BLL
{
    /// <summary>
    ///  用户配置管理类
    /// </summary>
    public class UserConfig
    {
        #region private field

        private static IsolatedStorageSettings _settings = IsolatedStorageSettings.ApplicationSettings;
        #endregion

        #region public property

        /// <summary>
        /// 是否自动播放声音，默认是允许,true
        /// </summary>
        public bool IsAutoPlayVoice
        {
            set
            {
                CreateOrEditSetting("IsAutoPlayVoice", value);
            }

            get
            {
                return GetSettingWithDefault("IsAutoPlayVoice", false);
            }
        }

        /// <summary>
        /// 是否显示词组
        /// </summary>
        public bool IsShowWordGroup
        {
            set
            {
                CreateOrEditSetting("IsShowWordGroup", value);
            }

            get
            {
                return GetSettingWithDefault("IsShowWordGroup", true);
            }
        }

        /// <summary>
        /// 是否显示同义词
        /// </summary>
        public bool IsShowSynonym
        {
            set
            {
                CreateOrEditSetting("IsShowSynonym", value);
            }

            get
            {
                return GetSettingWithDefault("IsShowSynonym", true);
            }
        }

        /// <summary>
        /// 是否显示例句
        /// </summary>
        public bool IsShowSentence
        {
            //set
            //{
            //    CreateOrEditSetting("IsShowSentence", value);
            //}

            get
            {
                return GetSettingWithDefault("IsShowSentence", true);
                //#if FREE
                //                return false;
                //#else
                //                return !SystemConfig.IsTrial && GetSettingWithDefault("IsShowSentence", true);
                //#endif
            }
        }

        /// <summary>
        /// 用户选中的书籍
        /// </summary>
        public int UserBookId
        {
            set
            {
                CreateOrEditSetting("UserBookId", value);
            }

            get
            {
                return GetSettingWithDefault("UserBookId", 0);
            }
        }

        /// <summary>
        /// 排序方式，0为顺序，1为乱序
        /// </summary>
        public int OrderType
        {
            set
            {
                CreateOrEditSetting("OrderType", value);
            }

            get
            {
                return GetSettingWithDefault("OrderType", 0);
            }
        }

        /// <summary>
        /// 复习熟词时间间隔（周），默认3周
        /// </summary>
        public int CompletedWordReviewDuration
        {
            set
            {
                CreateOrEditSetting("CompletedWordReviewDuration", value);
            }

            get
            {
                return GetSettingWithDefault("CompletedWordReviewDuration", 2);
            }
        }

        /// <summary>
        /// 持久化队列中的单词，只持久化一个即可
        /// </summary>
        public string SerielizeQueueWord
        {
            set
            {
                CreateOrEditSetting("SerielizeQueueWord", value);
            }

            get
            {
                return GetSettingWithDefault("SerielizeQueueWord", string.Empty);
            }
        }

        /// <summary>
        /// 最后提交统计的日期
        /// </summary>
        public string LastCalcCountDate
        {
            set
            {
                CreateOrEditSetting("LastCalcCountDate", value);
            }

            get
            {
                return GetSettingWithDefault("LastCalcCountDate", string.Empty);
            }
        }

        #endregion

        #region private method

        /// <summary>
        /// 返回独立存储设置的值，如果不存在，则返回默认值
        /// </summary>
        /// <returns></returns>
        private static T GetSettingWithDefault<T>(string key, T t)
        {
            T getValue;
            return !_settings.TryGetValue(key, out getValue) ? t : getValue;
        }

        /// <summary>
        /// 给独立存储的设置赋值，如果不存在该设置则创建一个
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void CreateOrEditSetting(string key, object value)
        {
            if (!_settings.Contains(key))
            {
                _settings.Add(key, value);
            }
            else
            {
                _settings[key] = value;
            }

            _settings.Save();
        }
        #endregion
    }
}
